from decimal import Decimal

students = []


def long():
    if len(students) == 0:
        print('信息为空，无法操作！')
        return 0
    else:
        return 1


def add_student():
    try:
        name = input("请输入学生姓名:")
        num = input("请输入学号:")
        chinese = Decimal(input("请输入语文成绩:"))
        english = Decimal(input("请输入英语成绩:"))
        math = Decimal(input("请输入数学成绩:"))
        student = {'name': name, 'num': num, 'chinese': chinese, 'english': english, 'math': math}
        students.append(student)
        print('录入完成！')
    except:
        print('输入有误！')


def del_student(mark):
    num = input("请输入要删除的学生学号:")
    for student in students:
        if student['num'] == num:
            students.remove(student)
            print('已删除!')
            mark = False
    if mark:
        print('未找到该学生')


def modify_student(mark):
    num = input("请输入要修改的学生学号:")
    for student in students:
        if student['num'] == num:
            print("请重新输入成绩：")
            student['chinese'] = Decimal(input("请输入语文成绩:"))
            student['english'] = Decimal(input("请输入英语成绩:"))
            student['math'] = Decimal(input("请输入数学成绩:"))
            print('修改完成！')
            mark = False
    if mark:
        print('未找到该学生')


def show_student(mark):
    num = input('请输入需要查询的学生学号，全部查询输入all:')
    if num == 'all':
        print("所有学生成绩信息如下：")
        for student in students:
            print(
                f"姓名: {student['name']} 学号: {student['num']} 语文成绩: {student['chinese']} 英语成绩: {student['english']} 数学成绩: {student['math']}")
    else:
        for student in students:
            if student['num'] == num:
                score = student['chinese'] + student['english'] + student['math']
                average = score / 3
                print(
                    f"姓名: {student['name']} 学号: {student['num']} 语文成绩: {student['chinese']} 英语成绩: {student['english']} 数学成绩: {student['math']}")
                print(f"总分: {score} 平均分: {average:.2f}")
                mark = False
        if mark:
            print('未找到该学生')


while True:
    mark = True
    choice = input("选择你要进行的操作: 1.录入学生成绩 2.删除学生成绩 3.修改学生成绩 4.查询学生成绩 5.退出程序:")
    if choice == "1":
        add_student()
    elif choice == "2":
        if long():
            del_student(mark)
    elif choice == "3":
        if long():
            modify_student(mark)
    elif choice == "4":
        if long():
            show_student(mark)
    elif choice == "5":
        print("已经退出程序！")
        exit()